package com.example.demo.test.l1;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: hhl
 * @createDate: 2023/12/1
 * @version: 1.0
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(16);
        //创建服务器
        ServerSocketChannel open = ServerSocketChannel.open();
        //绑定一个监听端口
        open.bind(new InetSocketAddress(8080));
        List<SocketChannel> channels = new ArrayList<>();
        //accept
        while (true){
            System.out.println("connecting....");
            SocketChannel accept = open.accept();
            System.out.println("connected..."+accept);
            channels.add(accept);
            for (SocketChannel channel:channels){
                System.out.println("before read..."+channel);
                channel.read(buffer);
                buffer.flip();
                byte b = buffer.get();
                System.out.println(b);
                buffer.clear();
                System.out.println("after read..."+channel);

            }
        }
    }
}
